<template>
  <div class="login-box">
    <div class="login-card">
      <h2>品优购登录</h2>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            type="username"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 5, max: 8, message: '用户名长度为5-8', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' },
          { min: 5, max: 8, message: '密码长度为5-8', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        try {
          if (valid) {
            // valid 为真 调用user.js函数
            await this.$store.dispatch('user/syncLogin', this.ruleForm)
            //登录成功提示
            this.$message.success('登录成功')
            //页面跳转
            this.$router.push('/layout')
          }
        } catch (error) { }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  },
  created () {
  },
  computed: {
  },

}
</script>
<style lang='less'  scoped>
.login-box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-color: #324a68;

  .login-card {
    width: 460px;
    height: 280px;
    border-radius: 3px;
    background-color: #fff;

    h2 {
      margin-top: 20px;
      text-align: center;
      font-weight: normal;
    }

    .el-form {
      padding: 0 30px;

      .el-form-item {
        margin-top: 25px;
      }
    }
  }
}
</style>
